#include "NonFinAutTest.h"
#include <list>
#include <map>
#include <iterator>
#include <ostream>
#include <fstream>

NonFinAut<string> NonFinAutTest::getNonFinAut()
{
	return nonFinAut;
}

void NonFinAutTest::makeDrawning(NonFinAut<string> &obj)
{
	//multimap<string, Transition<string>*> objTable = obj.getTransitionList();
    //multimap<string, Transition<string>*>::iterator itObj;
	list<Transition<string>> objTable = obj.getTransitionList();
	list<Transition<string>>::iterator itObj;
	list<string> StartStates = obj.getStartList();
    list<string>::iterator itStartStates;
	list<string> EndStates = obj.getFinishList();
    list<string>::iterator itEndStates;
    //string ErrorState = obj.getErrorState();
    ofstream myfile;
    myfile.open ("drawing");
    myfile << "digraph G { \n";
    bool combinedStartEnd=false;

    for (itEndStates=EndStates.begin(); itEndStates!=EndStates.end(); itEndStates++)
        myfile << *itEndStates << "[color=yellow style=filled]; \n";
//    if(ErrorState.length()>1)
  //      myfile << ErrorState << "[color=red style=filled]; \n";

    //kleuren van start/stop en error-states
    for (itStartStates=StartStates.begin(); itStartStates!=StartStates.end(); itStartStates++)
    {
        bool isEndState=false;
            for (itEndStates=EndStates.begin(); itEndStates!=EndStates.end(); itEndStates++)
            {
                if(*itStartStates==*itEndStates)
                {
                    //EndStates.erase(*itEndStates);
                    combinedStartEnd=true;
                    isEndState=true;
                    myfile << *itStartStates << "[color=green style=filled]; \n";
                    break;
                }

            }
            if(!isEndState)
                myfile << *itStartStates << "[color=blue style=filled]; \n";
    }


    //schrijven legenda!
    if(StartStates.size()>1)
        if(combinedStartEnd)
            myfile << "StartEndStates [shape = box color=green style=filled]; \n";
        else
            myfile << "StartStates [shape = box color=blue style=filled]; \n";
    else
        if(combinedStartEnd)
            myfile << "StartEndState [shape = box color=green style=filled]; \n";
        else
            myfile << "StartState [shape = box color=blue style=filled]; \n";
    if(EndStates.size()>1)
        myfile << "EndStates [shape = box color=yellow style=filled]; \n";
    else
        if(EndStates.size()==1)
            myfile << "EndState [shape = box color=yellow style=filled]; \n";
//    if(ErrorState.length()>1)
  //      myfile << "ErrorState [shape = box color=red style=filled]; \n";

    //schrijven states en transities
    for (itObj=objTable.begin(); itObj!=objTable.end(); itObj++)
		myfile << (*itObj).begin << "->" << (*itObj).end << "[taillabel=\"" << (*itObj).symbol << "\"]; \n";

    myfile << "} \n";
    myfile.close();
    //aanmaken drawing.png d.m.v. command line util
    system("dot -Kdot -Tpng -O drawing");
    //gebruik windows geregistreerde viewer voor png bestanden
    system("drawing.png");
}


NonFinAutTest::NonFinAutTest()
{
}

NonFinAutTest::~NonFinAutTest()
{
}

void NonFinAutTest::testProgram()
{
	cout<<"NDFA Test"<<endl << "==================================="<<endl<<endl;

	//NonFinAut<string> detFinAut;
	list<string> startList, finishList;
	//startList.push_front("B");
	startList.push_front("A");
	finishList.push_front("D");
	nonFinAut = NonFinAut<string>(startList, finishList);
	nonFinAut.addTransition("A", 'a', "B");
	nonFinAut.addTransition("A", 'b', "B");
	nonFinAut.addTransition("A", 'b', "Z");
	nonFinAut.addTransition("B", 'a', "Z");
	nonFinAut.addTransition("B", 'b', "C");
	nonFinAut.addTransition("B", 'a', "C");
	nonFinAut.addTransition("C", 'a', "Z");
	nonFinAut.addTransition("C", '$', "D");
	nonFinAut.addTransition("D", 'a', "D");
	nonFinAut.addTransition("D", 'b', "D");
	nonFinAut.addTransition("Z", 'a', "Z");
	nonFinAut.addTransition("Z", 'b', "Z");
	//detFinAut.rewrite();
	nonFinAut.printLanguage(5);
	if(nonFinAut.accept("a"))
	{
		cout << "string accepted" << endl;
	}
	else
	{
		cout << "string not accepted" << endl;
	}

	//makeDrawing(nonFinAut);

	cout <<endl<<endl;
}

void getTransitions()
{
	
}

void NonFinAutTest::getDrawning()
{
	NonFinAut<string> test = getNonFinAut();
	makeDrawning(test);
}

void NonFinAutTest::printTransitions()
{
	NonFinAut<string> test = getNonFinAut();
	list<Transition<string> > lijst= test.getTransitionList();
	for(list<Transition<string> >::iterator l = lijst.begin(); l != lijst.end(); l++)
	{
		cout << l->begin << " -> " << l->symbol << " -> " << l->end <<endl;
	}
}
